define(['angular',"text!www/shop/baiduMap.html",'url','common','baidumap'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
    function controller($scope,$rootScope,$http,$location){
    		initFooter(1);
    		hideMenu();
    		mapMenu();
//    		
//    		bottomMenu();
    		$rootScope.config.map = "shop";
    		var Maplist = function () {
    	    	 this.longitude = 0;
    	         this.latitude =0;
    	         this.speed = 0;
    	         this.accuracy = 0;
    	         this.items=[];
    	         this.busy = false;
    	     };
    		$scope.list = new Maplist();
    		
    		$scope.list.getWxInfo = function(){
    	    	 if (this.busy) return;
    	    	 this.busy = true;
    	         var host = $location.host()+"%2Fhtml%2Fchunle%2Findex.html%23%2Fmap_list";
    	         var url = cl.getWxInfoUrl+"&callback=JSON_CALLBACK&host="+host;
    	         var pro = this;
    	         $http.jsonp(url).success(function (res) {
    	        	 if(res.code=="CL02017"){
    	        		 wx.config({
    	        			    debug: false, 
    	        			    appId: res.result.appId,
    	        			    timestamp: res.result.timestamp,
    	        			    nonceStr: res.result.nonceStr,
    	        			    signature: res.result.signature,
    	        			    jsApiList: [
    	        			      // 所有要调用的 API 都要加到这个列表中
    	        			      'getLocation',
    	        			    ]
    	        			  });
    	        			  wx.ready(function () {
    	        				  wx.getLocation({
    	        					    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
    	        					    success: function (resu) {
    	        					    	pro.latitude = resu.latitude; // 纬度，浮点数，范围为90 ~ -90
    	        					    	cl.user.latitude = resu.latitude;
    	        					    	pro.longitude = resu.longitude; // 经度，浮点数，范围为180 ~ -180。
    	        					    	cl.user.longitude = resu.longitude;
    	        					    	pro.speed = resu.speed; // 速度，以米/每秒计
    	        					    	pro.accuracy = resu.accuracy; // 位置精度
    	        					    },
    	        					    complete : function(resu) {
    	        					    	pro.baiduMap();
    	        			            }
    	        					});
    	        			  });
    	        	 }
    	        	 pro.busy = false;
    	         });
    	    	 
    	     }
    		$scope.list.baiduMap = function(){
    	    	 var pro = this;
    	    	 var address = this.items;
    	    	// 百度地图API功能
    	    		var map = new BMap.Map("l-map");
    	    		if(this.longitude==0&&this.latitude==0){
    	    			var point = new BMap.Point(address[0].longitude,address[0].latitude);
    	    			map.centerAndZoom(point, 11);
    	    		}else{
    	    			var mypoint = new BMap.Point(this.longitude,this.latitude);
    	        		var myIcon = new BMap.Symbol(BMap_Symbol_SHAPE_POINT, {
    	        	    scale: 0,//图标缩放大小
    	        	    fillColor: "blue",//填充颜色
    	        	    fillOpacity: 0.5//填充透明度
    	        	  });
    	        		map.centerAndZoom(mypoint, 11);
    	    		}
    	    		map.disableDoubleClickZoom();
    	    	  // 添加带有定位的导航控件
    	    	  var navigationControl = new BMap.NavigationControl({
    	    	    // 靠左上角位置
    	    	    anchor: BMAP_ANCHOR_TOP_LEFT,
    	    	    // LARGE类型
    	    	    type: BMAP_NAVIGATION_CONTROL_LARGE,
    	    	    // 启用显示定位
    	    	    enableGeolocation: true
    	    	  });
    	    	  map.addControl(navigationControl);
    	    	  if(this.longitude==0&&this.latitude==0){
    	    		  
    	    	  }else{
    	    		  var markerME = new BMap.Marker(mypoint,{icon:myIcon});  // 创建标注
    	    		  map.addOverlay(markerME);         // 将标注添加到地图中
    	    		  
    	    		  var bLabel =new BMap.Label("所在位置",{offset:new BMap.Size(-16,-19)})
    	    			  
    	    		  markerME.setLabel(bLabel);
    	    		  
    	    		  bLabel.setStyle({ 
  	    				"border": "none",
  	    				"padding":"2px",
  	    				"border-radius":"2px",
  	    				"color":"black",
  	    				"opacity":"1",
  	    				"font-weight":"bold"
  	    				//"background":"none"
  	    				});
    	    		  
    	    	  }
    	    	  // 添加定位控件
    	    	  var geolocationControl = new BMap.GeolocationControl();
    	    	  geolocationControl.addEventListener("locationSuccess", function(e){
    	    		  // 定位成功事件
    	    		  if(markerME){
    	    			  map.removeOverlay(markerME);
    	    		  }
    	    		  pro.latitude = e.point.lat;
    	    		  pro.longitude = e.point.lng;
    	    	  });
    	    	  geolocationControl.addEventListener("locationError",function(e){
    	    	    // 定位失败事件
    	    	  });
    	    	  map.addControl(geolocationControl);
    	    		var index = 0;
    	    		var myGeo = new BMap.Geocoder();
    	    		for(var i = 0; i<address.length; i++){
    	    			var marker = new BMap.Marker(new BMap.Point(address[i].longitude,address[i].latitude));
    	    			map.addOverlay(marker);
    	    			var bLabel = new BMap.Label(address[i].storeName,{offset:new BMap.Size(-22,-19)});
    	    			bLabel.setStyle({ 
    	    				"border": "none",
    	    				"padding":"2px",
    	    				"border-radius":"2px",
    	    				"color":"black",
    	    				"opacity":"1",
    	    				"font-weight":"bold"
    	    				//"background":"none"
    	    				});
    	    			
    	    			marker.setLabel(bLabel);
    	    			marker.addEventListener("click",function(e){
    	    				$("#l-map").height("100%");
    	    				var p = e.target;
    	    				var add = getAdd(p.getPosition().lng,p.getPosition().lat,address);
//    	    				add._div = document.createElement("div");
//    	    				add._div.style.color = "red";
    	    				showShop(add);
    	    				addEvent(address);
    	    			});
    	    		}
    	    		hideSpin();
    	     }
    		
    		$scope.list.getMapList = function () {
    	         if (this.busy) return;
    	         this.busy = true;
    	         var url = cl.getMapListUrl+"&callback=JSON_CALLBACK";
    	         var pro = this;
    	         $http.jsonp(url).success(function (res) {
    	        	 pro.busy = false;
    	        	 if(res.code=="CL02018"){
    	        		 pro.items = res.result;
    	        		 if(!testProject){
    	        			 pro.getWxInfo();//正式
    	            	 }else{
    	            		 pro.baiduMap();//测试所用
    	            	 }
    	        	 }else if(res.code=="CL020019"){
    	        		 
    	        	 }
    	         });
    	     };
    		//获取用户信息和店面信息
    		$scope.list.getMapList();
    		$scope.visible = true;
    		$scope.hideMapInfo = function()
    		{
    				$("#l-info").hide();
    		};
    		
    };
    return {controller:controller,tpl:tpl};
});

function getAdd(long,lat,address){
	for( var o in address){
		if(typeof(address[o].longitude)=="string"){
			address[o].longitude = Number(address[o].longitude);
		}
		if(typeof(address[o].latitude)=="string"){
			address[o].latitude = Number(address[o].latitude);
		}
		if(address[o].longitude===long&&address[o].latitude===lat){
			return address[o];
		}
	}
}
function getAddById(id,address){
	for( var o in address){
		if(address[o].id==id){
			return address[o];
		}
	}
}
function addEvent(address){
	$("#lead-address").click(function(){
			var id = $("#addid").val();
			var add = getAddById(id,address);
			var url = "http://api.map.baidu.com/marker?location=";
			var location = add.latitude+","+add.longitude;
		//	var location = '23.477631,113.173263';
			var content='地址：'+add.address;
			var title = add.storeName;
			//console.info(url+location+"&title="+title+"&content="+content+"&output=html&src=chunle");
			window.location.href = url+location+"&title="+title+"&content="+content+"&output=html&src=chunle";
		})
}

function removeEvent(){
	$("#l-info").removeEventListener("click");
}
function showShop(add){	
	//var content = '<div style="margin:0;line-height:20px;padding:2px;">店铺名：'+add.storeName+'<br/>地址：'+add.address+'<br/>电话：'+add.tel+'</div><input type="hidden" id="addid" value="'+add.id+'"></input>';
	$("#shop-name").html(add.storeName);
	$("#shop-address").html("地址："+add.address);
	$("#shop-phone").html("电话："+add.tel);
	$("#lead-phone").attr("href","tel:"+add.tel);
	$("#addid").val(add.id);
	
	//$("#l-info").append(content);
	$("#l-info").show();
}